<?php
namespace app\index\controller;

use app\Logined;
use think\facade\Config;

class Baidu extends Logined
{
    /**
     * Notes:百度语音转文字
     */
    public function voice_to_text(){
        $client = new \GuzzleHttp\Client(['verify' => false ]);
        $baidumap_key = 'mOReRfpt5lUtIaxN4GNOgCDt';
        $baidumap_secret = 'TqwP6qnGROQczKjNyWFMug0DPGUuspQb';
        $res = $client->request('GET', "https://aip.baidubce.com/oauth/2.0/token?client_id={$baidumap_key}&client_secret={$baidumap_secret}&grant_type=client_credentials");
        $response=$res->getBody()->getContents();
        if(empty($response)){
            return $this->ajax_return(0,'获取token失败',[
                '$baidumap_key'=>$baidumap_key,
                '$response'=>$response,
            ]);
        }
        $response=json_decode($response,true);
        if(empty($response['access_token'])){
            return $this->ajax_return(0,'获取token失败2',[
                '$baidumap_key'=>$baidumap_key,
                '$response'=>$response,
            ]);
        }
        $access_token = $response['access_token'];

        $file = $this->request->file('file');
        if(empty($file)){
            return $this->error('文件不能为空！');
        }
        $savename = \think\facade\Filesystem::putFile( 'upload', $file);
        $savename=str_replace("\\",'/',$savename);
//        $savename='static/16k.pcm';

        $fh = fopen(PROJECT_ROOT."/$savename", "rb");//rb是兼容二进制读取
        $data = fread($fh, filesize(PROJECT_ROOT."/$savename"));
        fclose($fh);

        $res = $client->request('POST', "http://vop.baidu.com/server_api?cuid=aics_app&token={$access_token}",[
            'headers'=>[
                'Content-Type'=>'audio/pcm;rate=16000',
                'format'=>'pcm',
            ],
            'body'=>$data
        ]);
        $response2=$res->getBody()->getContents();

        return $this->success('请求成功',[
            '$access_token'=>$access_token,
            '$response2'=>$response2,
            '$savename'=>$savename,
            'PROJECT_ROOT'=>PROJECT_ROOT."/$savename",
//            '$data'=>$data,
        ]);
    }
    /**
     * Notes:百度经纬度转地址
     */
    public function latlng_to_addr(){
        $baidumap_key = Config::get('app.baidumap_key');
        $client = new \GuzzleHttp\Client(['verify' => false ]);
        $res = $client->request('GET', "https://api.map.baidu.com/reverse_geocoding/v3/?ak={$baidumap_key}&output=json&poi_types=房地产&language_auto=1&extensions_road=true&radius=200&extensions_poi=1&coordtype=wgs84ll&location={$this->param['lat']},{$this->param['lng']}");
        $response=$res->getBody()->getContents();
        if(empty($response)){
            return $this->ajax_return(2,'解析失败1',[
                'param'=>$this->param,
                '$baidumap_key'=>$baidumap_key,
                '$response'=>$response,
            ]);
        }
        $response=json_decode($response,true);
        if($response['status']!==0){
            return $this->ajax_return(2,'解析失败2',[
                'param'=>$this->param,
                '$baidumap_key'=>$baidumap_key,
                '$response'=>$response,
            ]);
        }

        if(!empty($response['result']['pois'])){
            $formatted_address=$response['result']['pois'][0]['addr'].$response['result']['pois'][0]['name'].$response['result']['pois'][0]['direction'].$response['result']['pois'][0]['distance'].'米';
        }else{
            $formatted_address=$response['result']['formatted_address'];
        }

        return $this->success('解析成功',[
            '$response'=>$response,
            'addr'=>$response['result'],
            'formatted_address'=>$formatted_address,
        ]);
    }

}
